// pages/integral/order-detail/order-detail.ts
import { getIntegralOrderDetail, takeIntegralOrder, deleteIntegralOrder, getIntegralOrderExpress } from '../../../api/points_mall.js';
import { PAGES, goToPage } from '../../../config/pages.js';

Page({
  data: {
    orderId: '',
    orderInfo: {},
    loading: true,
    showExpress: false,
    expressInfo: {}
  },

  onLoad(options: any) {
    this.setData({ orderId: options.orderId });
    this.getOrderDetail();
  },

  // 获取订单详情
  async getOrderDetail() {
    try {
      this.setData({ loading: true });
      const res = await getIntegralOrderDetail(this.data.orderId);
      this.setData({
        orderInfo: res.data,
        loading: false
      });
    } catch (error) {
      console.error('获取订单详情失败:', error);
      wx.showToast({
        title: '加载失败',
        icon: 'none',
      });
      this.setData({ loading: false });
    }
  },

  // 确认收货
  async onTakeOrder() {
    wx.showModal({
      title: '确认收货',
      content: '确认已收到商品？',
      success: async (res) => {
        if (res.confirm) {
          try {
            wx.showLoading({ title: '处理中...' });
            await takeIntegralOrder({ order_id: this.data.orderId });
            
            wx.showToast({
              title: '收货成功',
              icon: 'success',
            });
            
            // 重新加载订单详情
            this.getOrderDetail();
          } catch (error) {
            console.error('确认收货失败:', error);
            wx.showToast({
              title: error.message || '操作失败',
              icon: 'none',
            });
          } finally {
            wx.hideLoading();
          }
        }
      }
    });
  },

  // 删除订单
  async onDeleteOrder() {
    wx.showModal({
      title: '删除订单',
      content: '确定要删除这个订单吗？',
      success: async (res) => {
        if (res.confirm) {
          try {
            wx.showLoading({ title: '删除中...' });
            await deleteIntegralOrder({ order_id: this.data.orderId });
            
            wx.showToast({
              title: '删除成功',
              icon: 'success',
            });
            
            setTimeout(() => {
              wx.navigateBack();
            }, 1500);
          } catch (error) {
            console.error('删除订单失败:', error);
            wx.showToast({
              title: error.message || '删除失败',
              icon: 'none',
            });
          } finally {
            wx.hideLoading();
          }
        }
      }
    });
  },

  // 查看物流
  async onViewExpress() {
    try {
      wx.showLoading({ title: '加载中...' });
      const res = await getIntegralOrderExpress(this.data.orderId);
      this.setData({
        showExpress: true,
        expressInfo: res.data
      });
    } catch (error) {
      console.error('获取物流信息失败:', error);
      wx.showToast({
        title: error.message || '获取物流信息失败',
        icon: 'none',
      });
    } finally {
      wx.hideLoading();
    }
  },

  // 隐藏物流信息
  hideExpress() {
    this.setData({ showExpress: false });
  },

  // 复制订单号
  copyOrderId() {
    wx.setClipboardData({
      data: this.data.orderId,
      success: () => {
        wx.showToast({
          title: '已复制',
          icon: 'success',
        });
      }
    });
  },

  // 获取订单状态文本
  getStatusText(status: number) {
    const statusMap: { [key: number]: string } = {
      0: '待支付',
      1: '已支付',
      2: '已发货',
      3: '已收货',
      4: '已完成',
      '-1': '已取消'
    };
    return statusMap[status] || '未知状态';
  },

  // 获取订单状态样式
  getStatusClass(status: number) {
    const classMap: { [key: number]: string } = {
      0: 'status-pending',
      1: 'status-paid',
      2: 'status-shipped',
      3: 'status-received',
      4: 'status-completed',
      '-1': 'status-cancelled'
    };
    return classMap[status] || 'status-unknown';
  }
});
